package com.carleasoft.mps.cms.business.leave.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carleasoft.mps.cms.business.leave.bean.entity.LeaveEntity;
import com.carleasoft.mps.cms.business.leave.bean.entity.ReportEntity;
import com.carleasoft.mps.cms.business.leave.bean.po.*;
import com.carleasoft.mps.cms.business.leave.bean.vo.*;
import com.carleasoft.mps.cms.business.leave.service.LeaveService;
import com.carleasoft.mps.cms.business.leave.service.ReportService;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.core.utils.IBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 请/销 假
 *
 * @author: yangHao
 * @createDate:2023/6/3
 * @description:
 */
@RestController
@RequestMapping("/leave")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;
    @Autowired
    private ReportService reportService;

    /**
     * 请假新增/编辑
     *
     * @param leaveUserPo
     * @return
     */
    @PostMapping("/saveLeave")
    public R save(@RequestBody LeaveUserPo leaveUserPo) {
        IBeanUtil.paramVerify(leaveUserPo, "userList", "reasons", "type", "startTime", "endTime", "duration");
        String code = leaveService.saveLeave(leaveUserPo);
        return R.ok(code);
    }


    /**
     * 分页-请假
     *
     * @param leavePo
     * @return
     */
    @PostMapping("/pageQueryLeave")
    public R pageQueryLeave(@RequestBody LeavePo leavePo) {
        Page<LeaveUserPo> leaveEntity = leaveService.pageQuery(new Page(leavePo.getCurrent(), leavePo.getSize()), leavePo);
        return R.ok(leaveEntity);
    }


    /**
     * 删除-请假
     *
     * @param leaveEntity
     * @return
     */
    @PostMapping("/deleteLeave")
    public R deleteLeave(@RequestBody LeaveEntity leaveEntity) {
        IBeanUtil.paramVerify(leaveEntity, "leaveCode");
        boolean isOk = leaveService.delete(leaveEntity.getLeaveCode());
        return R.ok(isOk);
    }


    /**
     * 修改状态-请假
     *
     * @param leaveEntity
     * @return
     */
    @PostMapping("/updateState")
    public R updateState(@RequestBody LeaveEntity leaveEntity) {
        IBeanUtil.paramVerify(leaveEntity, "leaveCode", "status");
        boolean isOk = leaveService.updateState(leaveEntity);
        return R.ok(isOk);
    }

    /**
     * 查询请假列表
     *
     * @return
     */
    @PostMapping("/listLeave")
    public R listLeave() {
        List<LeaveEntity> leaveEntities = leaveService.listLeave();
        return R.ok(leaveEntities);
    }

    /**
     * 根据请假编码查询人
     *
     * @param leaveEntity
     * @return
     */
    @PostMapping("/userList")
    public R userList(@RequestBody LeaveEntity leaveEntity) {
        IBeanUtil.paramVerify(leaveEntity, "leaveCode");
        List<UserListVo> userListVos = leaveService.userList(leaveEntity.getLeaveCode());
        return R.ok(userListVos);
    }


    /**
     * 销假 新增/编辑
     *
     * @param reportPo
     * @return
     */
    @PostMapping("/saveReport")
    public R saveReport(@RequestBody ReportPo reportPo) {
        IBeanUtil.paramVerify(reportPo, "leaveCode", "reasons", "startTime", "endTime", "duration");
        String code = reportService.saveReport(reportPo);
        return R.ok(code);

    }

    /**
     * 修改状态-销假
     *
     * @param reportEntity
     * @return
     */
    @PostMapping("/updateStatusReport")
    public R updateStatusReport(@RequestBody ReportEntity reportEntity) {
        IBeanUtil.paramVerify(reportEntity, "reportCode", "status");
        boolean isOk = reportService.updateStatusReport(reportEntity);
        return R.ok(isOk);
    }


    /**
     * 分页-销假
     *
     * @param leavePo
     * @return
     */
    @PostMapping("/pageQueryReport")
    public R pageQueryReport(@RequestBody LeavePo leavePo) {
        Page<ReportEntity> reportEntityPage = reportService.pageQueryReport(new Page(leavePo.getCurrent(), leavePo.getSize()), leavePo);
        return R.ok(reportEntityPage);
    }

    /**
     * 删除
     *
     * @param reportEntity
     * @return
     */
    @PostMapping("/deleteReport")
    public R deleteReport(@RequestBody ReportEntity reportEntity) {
        IBeanUtil.paramVerify(reportEntity, "reportCode");
        boolean isOk = reportService.deleteReport(reportEntity.getReportCode());
        return R.ok(isOk);
    }


    /**
     * 请假销假
     *
     * @return
     */
    @PostMapping("/countLeaveAndReport")
    public R countLeaveAndReport() {
        CountLeaveAndReportVo countLeaveAndReportVo = reportService.countLeaveAndReport();
        return R.ok(countLeaveAndReportVo);
    }

    /**
     * 出勤统计
     *
     * @param attendancePo
     * @return
     */
    @PostMapping("/attendance")
    public R attendance(@RequestBody AttendancePo attendancePo) {
        IBeanUtil.paramVerify(attendancePo, "year");
        Page<AttendanceVo> attendanceVoPage = reportService.attendance(new Page(attendancePo.getCurrent(), attendancePo.getSize()), attendancePo);
        return R.ok(attendanceVoPage);
    }

    /**
     * 年度请销假时长统计
     *
     * @return
     */
    @PostMapping("/yearCount")
    public R yearCount(@RequestBody YearCountPo yearCountPo) {
        YearCountVo yearCountVo = leaveService.yearCount(yearCountPo);
        return R.ok(yearCountVo);
    }

    /**
     * 请假类型统计
     * @return
     */
    @PostMapping("/leaveTypeCount")
    public R leaveTypeCount() {
        LeaveTypeCountVo leaveTypeCountVo = leaveService.leaveTypeCount();
        return R.ok(leaveTypeCountVo);
    }


}
